package cn.iocoder.yudao.module.resource.controller.admin.cosplay;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.resource.controller.admin.cosplay.vo.LmCosplayPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.cosplay.vo.LmCosplayRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.cosplay.vo.LmCosplaySaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.cosplay.LmCosplayDO;
import cn.iocoder.yudao.module.resource.service.cosplay.LmCosplayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 -CosPlay角色")
@RestController
@RequestMapping("/lm/cosplay")
@Validated
public class LmCosplayController {

    @Resource
    private LmCosplayService cosplayService;

    @PostMapping("/create")
    @Operation(summary = "创建角色")
    @PreAuthorize("@ss.hasPermission('lm:cosplay:create')")
    public CommonResult<Integer> createCosplay(@Valid @RequestBody LmCosplaySaveReqVO createReqVO) {
        return success(cosplayService.createCosplay(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新角色")
    @PreAuthorize("@ss.hasPermission('lm:cosplay:update')")
    public CommonResult<Boolean> updateCosplay(@Valid @RequestBody LmCosplaySaveReqVO updateReqVO) {
        cosplayService.updateCosplay(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('lm:cosplay:delete')")
    public CommonResult<Boolean> deleteCosplay(@RequestParam("id") Integer id) {
        cosplayService.deleteCosplay(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lm:cosplay:query')")
    public CommonResult<LmCosplayRespVO> getCosplay(@RequestParam("id") Integer id) {
        LmCosplayDO cosplay = cosplayService.getCosplay(id);
        LmCosplayRespVO lmCosplayRespVO = BeanUtils.toBean(cosplay, LmCosplayRespVO.class);
        cosplayService.setLmCosplayResp(lmCosplayRespVO);
        return success(lmCosplayRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色分页")
    @PreAuthorize("@ss.hasPermission('lm:cosplay:query')")
    public CommonResult<PageResult<LmCosplayRespVO>> getCosplayPage(@Valid LmCosplayPageReqVO pageReqVO) {
        PageResult<LmCosplayDO> pageResult = cosplayService.getCosplayPage(pageReqVO);
        PageResult<LmCosplayRespVO> voPageResult = BeanUtils.toBean(pageResult, LmCosplayRespVO.class);
        for (LmCosplayRespVO respVO : voPageResult.getList()) {
            cosplayService.setLmCosplayResp(respVO);
        }
        return success(voPageResult);
    }

}